import { ICategoryItem } from '@/types/goods'
import { IResponse } from '@/types/data'
import request from '@/utils/request'
import { defineStore } from 'pinia'
import { topCategory } from '../constants'

//加工成数组里含对象的形式,不需要经过接口请求瞬间就出来了,根本一闪都不会闪,无论网络有多慢
const defaultCategory = topCategory.map((item) => ({
  name: item,
}))

//创建模块
export default defineStore('category', {
  state: () => ({
    // list: [] as ICategoryItem[], //装分类数据的
    list: defaultCategory as ICategoryItem[], //装分类数据的
  }),
  actions: {
    async getCategoryList() {
      //每一个ICategoryItem对象组成最终数组Array(9)
      const r = await request.get<IResponse<ICategoryItem[]>>(
        '/home/category/head'
      )
      console.log(r)
      console.log(defaultCategory)

      this.list = r.data.result
    },
  },
})
